﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcctrueWMS.Foundation.Data;
using System.IO;
using System.Xml.Linq;

namespace AcctrueWMS.Foundation.Web.Cache
{
    class CacheManager : ICacheManager
    {

        public void LoadFromFile()
        {
            throw new NotImplementedException();
        }

        public void LoadFromFile(string path)
        {
            if (File.Exists(path))
            {
                XDocument xmlCache = XDocument.Load(path);
                IEnumerable<XElement> xNode = xmlCache.Root.Elements();
                foreach (XElement xe in xNode)
                {
                    string ifadd = xe.Attribute("ifadd") == null ? string.Empty : xe.Attribute("ifadd").Value;
                    string cacheKey = xe.Attribute("key") == null ? string.Empty : xe.Attribute("key").Value;
                    string str_cacheValue = xe.Attribute("value") == null ? string.Empty : xe.Attribute("value").Value;
                    Object cacheValue = CommandExprFactory.Eval(str_cacheValue, Runtime.Context);
                    if (ifadd.Trim() == "true")
                    {
                        Runtime.Cache[cacheKey] = cacheValue;
                    }
                }
            }
        }
    }
}
